package com.dw.etl.core.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dw.etl.core.bean.vo.LayMsg;
import com.dw.etl.core.bean.vo.Msg;
import com.dw.etl.core.bean.model.SysPermissions;
import com.dw.etl.core.bean.model.SysRolesPermissions;
import com.dw.etl.core.mapper.SysPermissionsMapper;
import com.dw.etl.core.mapper.SysRolesPermissionsMapper;
import com.dw.etl.core.api.PermissionsService;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author GuoPeng
 * @Title: PermissionsServiceImpl
 * @Description: TODO
 * @date 2019/5/13 21:07
 */
@Service
@Transactional
public class PermissionsServiceImpl implements PermissionsService {


    @Resource
    private SysPermissionsMapper sysPermissionsMapper;

    @Resource
    private SysRolesPermissionsMapper sysRolesPermissionsMapper;

    @Override
    public LayMsg getPermissionsList(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        Page<SysPermissions> sysPermissions = (Page<SysPermissions>) sysPermissionsMapper.selectall();
        return LayMsg.success().addCount(sysPermissions.getTotal()).addData(sysPermissions.getResult());
    }

    @Override
    public Msg updatePermissions(SysPermissions sysPermissions) {
        int i = sysPermissionsMapper.updateByPrimaryKeySelective(sysPermissions);
        if (i > 0) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    @Override
    public Msg PermissionsLock(SysPermissions sysPermissions) {
        int i = sysPermissionsMapper.updateByPrimaryKeySelective(sysPermissions);
        if (i > 0) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }

    @Override
    public Msg delectPermissions(Integer id) {
        int i = sysPermissionsMapper.deleteByPrimaryKey(id);
        if (i > 0) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }


    @Override
    public Msg selectAll(Integer id) {
        List<SysPermissions> selectall = sysPermissionsMapper.selectall();
        List<SysRolesPermissions> sysRolesPermissions = null;
        ArrayList list = new ArrayList();
        HashMap parm = new HashMap();
        parm.put("role_id", id);
        for (int n = 0; n < selectall.size(); n++) {
            sysRolesPermissions = sysRolesPermissionsMapper.selectByRolesidAndPermissionsid(parm);
            for (int m = 0; m < sysRolesPermissions.size(); m++) {
                if (sysRolesPermissions.get(m).getPermissionId() == selectall.get(n).getId()) {
                    HashMap data = new HashMap();
                    data.put("name", selectall.get(n).getPermission());
                    data.put("id", selectall.get(n).getId());
                    data.put("star", true);
                    list.add(data);
                    selectall.remove(n);
                }
            }

        }

        for (int c = 0; c < selectall.size(); c++) {
            HashMap data = new HashMap();
            data.put("name", selectall.get(c).getPermission());
            data.put("id", selectall.get(c).getId());
            data.put("star", false);
            list.add(data);
        }
        return Msg.success().add("info", list);
    }

    @Override
    public Msg RolesSetPermissions(Map map, Integer id) {
        /*全部移除*/
        HashMap parm = new HashMap();
        parm.put("role_id", id);
        List<SysRolesPermissions> sysRolesPermissions2 = sysRolesPermissionsMapper.selectByRolesidAndPermissionsid(parm);
        for (int i = 0; i < sysRolesPermissions2.size(); i++) {
            sysRolesPermissionsMapper.deleteByPrimaryKey(sysRolesPermissions2.get(i).getId());
        }


        Iterator it = map.entrySet().iterator();
        /*添加角色*/
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            HashMap parm0 = new HashMap();
            parm0.put("role_id", id);
            System.out.println(Integer.parseInt(entry.getKey().toString()));
            parm0.put("permission_id", Integer.parseInt(entry.getKey().toString()));
            List<SysRolesPermissions> sysRolesPermissions = sysRolesPermissionsMapper.selectByRolesidAndPermissionsid(parm0);
            if (sysRolesPermissions.size() < 1) {
                SysRolesPermissions sysRolesPermissions1 = new SysRolesPermissions();
                sysRolesPermissions1.setRoleId(id);
                sysRolesPermissions1.setPermissionId(Integer.parseInt(entry.getKey().toString()));
                int insert = sysRolesPermissionsMapper.insert(sysRolesPermissions1);

            }
        }
        return Msg.success();
    }

    @Override
    public Msg insertPermissions(SysPermissions sysPermissions) {
        int i = sysPermissionsMapper.insert(sysPermissions);
        if (i > 0) {
            return Msg.success();
        } else {
            return Msg.fail();
        }
    }
}
